package main

import (
	"encoding/base64"
	"encoding/binary"
	"encoding/json"
	"fmt"
	"node-go-ipc/src_go/namedpipe"
)

const pipeName = "demo_pipe___"

func main() {
	conf := &namedpipe.ServerConf{
		ReqCompress:     false,
		RespCompress:    false,
		RespCompressLev: 0,
	}

	inst := &sever{}
	namedpipe.StartNamedPipeServer(pipeName, conf, readyCallback, inst)
}

func readyCallback() {
	fmt.Printf("named pipe server started on %s", namedpipe.CreateFullPipeName(pipeName))
}

type sever struct {
}

func (s *sever) Handle(cont []byte, bins [][]byte, writer namedpipe.Writer) {
	fmt.Println("---------- get req info -------------")
	fmt.Printf("request body %s\n", string(cont))
	if 0 < len(bins) {
		for ind, bin := range bins {
			if 0 == ind {
				num := binary.BigEndian.Uint32(bin)
				fmt.Printf("the first binary content is s number %v\n", num)
				continue
			}
			if 1 == ind {
				fmt.Printf("the second binary content is a utf8 string %s\n", string(bin))
				continue
			}

		}
	}

	resp := &Resp{}
	resp.ReqId = base64.StdEncoding.EncodeToString(writer.GetOriginReqId())
	bs, _ := json.Marshal(resp)

	resultBins := make([][]byte, 1, 1)
	resultBins[0] = []byte("文字二进制内容")
	writer.WriteWithBins(bs, resultBins)
	//writer.Write(bs)

}

type Resp struct {
	ReqId string
}
